package com.zivwu.library.widget.banner;

import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import net.lucode.hackware.magicindicator.abs.IPagerNavigator;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;


public abstract class BannerAdapter<T> extends PagerAdapter {
    private List<View> views;
    private IPagerNavigator iPagerNavigator;

    public BannerAdapter(List<T> dates, ViewPager parent) {
        this(dates, parent, -1);
    }

    public BannerAdapter(List<T> dates, ViewPager parent, int resource) {
        LinkedList<T> ts = new LinkedList<>(dates);
        this.views = new ArrayList<View>();
        createView(parent, resource, ts);
        iPagerNavigator = instanceNavigator(ts);
    }

    protected abstract
    @NonNull
    IPagerNavigator instanceNavigator(LinkedList<T> ts);

    public IPagerNavigator getNavigator() {
        return iPagerNavigator;
    }

    /**
     * 创建viewpager内部所需要展示的画面
     *
     * @param parent   ViewPager
     * @param resource custom  LayoutRes
     * @param ts       添加了首尾的数据
     */
    private void createView(ViewPager parent, @LayoutRes int resource, LinkedList<T> ts) {
        for (T t : ts) {
            View inflate;
            if (resource == -1) {
                inflate = createImageView(parent);
            } else {
                inflate = LayoutInflater.from(parent.getContext()).inflate(resource, parent, false);
            }
            onInflateView(t, inflate);
            views.add(inflate);
        }

    }

    public abstract void onInflateView(T t, View view);

    private View createImageView(ViewPager parent) {
        ImageView imageView = new ImageView(parent.getContext());
        imageView.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
        ));
        return imageView;
    }


    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }


    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }


    @Override
    public void destroyItem(ViewGroup container, int position,
                            Object object) {
        container.removeView(views.get(getRealPosition(position)));
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = views.get(getRealPosition(position));
        container.addView(view);
        return view;
    }

    public int getRealPosition(int position) {
        return position % views.size();
    }

    public int getFistPosition() {
        int i = getCount()/2 % views.size();
        return getCount()/2 - i;
    }

    public int getRealSize(){
        return  views.size();
    }


}

